package cn.t212.itrip.auth.controller;

import cn.t212.itrip.auth.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.Map;

@ResponseBody
@Controller
public class DemoController {
    @Resource
    private UserService userService;

    @RequestMapping(value = "/hello", method = RequestMethod.GET)
    public String hello() {
        return "Hello world";
    }

    @RequestMapping(value = "/api/ckusr", method = RequestMethod.GET)
    public Dto chusr(String name) {
        Dto dto = new Dto();
        if("aa@qq.com".equals(name)) {
            dto.setSuccess("true");
            dto.setMsg("可以注册");
            dto.setErrorCode("0");
        } else {
            dto.setSuccess("false");
            dto.setMsg("已经存在");
            dto.setErrorCode("1000001");
        }
        return dto;
    }
    @RequestMapping(value = "/api/doregister", method = RequestMethod.POST)
    public Dto doregister(@RequestBody Map<String, String> map) {
        System.out.println(map);
        //调用用户的业务类的邮箱注册的方法
            //1 保存到数据库（用户未激活）
            //2 发送激活码到用户注册的邮箱(javamail)
            //3 保存激活码到redis数据库，并指定有效时长
        Dto dto = new Dto();
        dto.setSuccess("true");
        dto.setMsg("注册成功");
        dto.setErrorCode("0");
        return dto;
    }
    @RequestMapping(value = "/api/activate", method = RequestMethod.PUT)
    public Dto activate(String user, String code) {
        System.out.println(user + ":" + code);
        //调用用户的业务类的激活的方法
            //1 验证存在redis中的验证码是否跟用户的一致
                //是， 更新用户的状态为激活
                //否，返回错误
        Dto dto = new Dto();
        dto.setSuccess("true");
        dto.setMsg("激活成功");
        dto.setErrorCode("0");
        return dto;
    }
}
